package runtimeexec;
import java.util.*;
import java.io.*;

class StreamHandler extends Thread {
	InputStream is;
	Type type;

	enum Type {
		ERR, OUT
	};

	StreamHandler(InputStream is, Type type) {
		this.is = is;
		this.type = type;
	}

	public void run() {
		try {
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			switch (type) {
			case ERR:
				while ((line = br.readLine()) != null) {
					System.err.println(">" + line);
				}
				break;
			case OUT:
				while ((line = br.readLine()) != null) {
					System.out.println(">" + line);
				}
				break;
			default:
			}

		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
}

public class GoodWindowsExec {
	public static void main(String args[]) {
		String cmd;
		Runtime rt = Runtime.getRuntime();
		Scanner sc = new Scanner(System.in);
		Process proc;
		try {
			do {
				System.out.print(">");
				cmd = sc.nextLine();
				proc = rt.exec("cmd " + cmd);
				StreamHandler errorStream = new StreamHandler(
						proc.getErrorStream(), StreamHandler.Type.ERR);
				StreamHandler outputStream = new StreamHandler(
						proc.getInputStream(), StreamHandler.Type.OUT);
				errorStream.start();
				outputStream.start();
				
			} while (!cmd.equalsIgnoreCase("exit"));
			System.out.println("Exiting...");
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
	}
}